#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@FileName  :views2.py 
@desc      :
@Author    :Young
@IDE       :PyCharm
@Time      :2022-02-17 14:08
'''

import datetime
import random

from rest_framework import status
from rest_framework.generics import GenericAPIView, ListCreateAPIView, CreateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from Web1.models import FamilyTroubles, FamilyTroublesResponse,\
    StudyPressure, StudyPressureResponse, WorkPressure, WorkPressureResponse, EmotionsTroubles, EmotionsTroublesResponse,\
    JieYou,PsychologicalConsultant
from Web1.serializers import FamilyTroublesSerializer, FamilyTroublesResponseSerializer, WorkPressureResponseSerializer, \
    WorkPressureSerializer, EmotionsTroublesSerializer, EmotionsTroublesResponseSerializer, StudyPressureSerializer,\
    StudyPressureResponseSerializer, JieyouSerializer, PsychologicalConsultantSerializer


class Web1GetJieYouView(GenericAPIView):
    queryset = JieYou.objects.all()
    serializer_class = JieyouSerializer

    def get(self, request, pk):
        # 随机返回一篇解忧相关的文章
        jieyouArticle = self.get_object()
        serializer = self.get_serializer(jieyouArticle)


        return Response({
            'code':200,
            'msg':'ok',
            'data':serializer.data
        })
class Web1GetFamilyTroublesView(GenericAPIView):
    queryset = FamilyTroubles.objects.all()
    serializer_class = FamilyTroublesSerializer
    def get(self,*args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset,many=True)

        return Response({
            'code': 200,
            'msg':'ok',
            'data':serializer.data
        })
class Web1PushFamilyTroublesView(GenericAPIView):
    queryset = FamilyTroubles.objects.all()
    serializer_class = FamilyTroublesSerializer
    def post(self,request):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            data2 = FamilyTroubles(id=None, uid=data['uid'], content=data['content'], publish_date=data['publish_date'])
            # print(data2)
            data2.save()

            return Response({
                'code':200,
                'msg':'ok'
            })
        else:
            return Response({
                'code':404,
                'msg':serializer.error
            })

class Web1GetFamilyTroublesResponseView(GenericAPIView):
    queryset = FamilyTroublesResponse.objects.all()
    serializer_class = FamilyTroublesResponseSerializer
    def get(self,request,pk):
        # print(pk)
        # trouble信息
        trouble_info = FamilyTroubles.objects.filter(id=pk).first()
        serializer1 = FamilyTroublesSerializer(trouble_info)
        # 该trouble相关回复信息
        trouble_response_info = FamilyTroublesResponse.objects.filter(family_troubles_id=pk)
        serializer2 = self.get_serializer(trouble_response_info, many=True)


        return Response({
            'code': 200,
            'msg':'ok',
            'data':{
                'info':serializer1.data,
                'response_info':serializer2.data
            }
        })
class Web1PushFamilyTroublesResponseView(GenericAPIView):
    queryset = FamilyTroublesResponse.objects.all()
    serializer_class = FamilyTroublesResponseSerializer
    def post(self,request):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            # print(data)
            data2 = FamilyTroublesResponse(id=None, uid=data['uid'], family_troubles_id=data['family_troubles_id'],
                                           content=data['content'], up_count=data['up_count'], publish_date=data['publish_date'])
            # print(data2)
            data2.save()

            return Response({
                'code':200,
                'msg':'ok'
            })
        else:
            return Response({
                'code':404,
                'msg':serializer.error
            })

class Web1GetEmotionTroublesView(GenericAPIView):
    queryset = EmotionsTroubles.objects.all()
    serializer_class = EmotionsTroublesSerializer
    def get(self,*args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset,many=True)

        return Response({
            'code': 200,
            'msg':'ok',
            'data':serializer.data
        })
class Web1PushEmotionTroublesView(GenericAPIView):
    queryset = EmotionsTroubles.objects.all()
    serializer_class = EmotionsTroublesSerializer
    def post(self,request):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            data2 = EmotionsTroubles(id=None, uid=data['uid'], content=data['content'], publish_date=data['publish_date'])
            data2.save()
            return Response({
                'code':200,
                'msg':'ok'
            })
        else:
            return Response({
                'code':404,
                'msg':serializer.error
            })
class Web1GetEmotionTroublesResponseView(GenericAPIView):
    queryset = EmotionsTroublesResponse.objects.all()
    serializer_class = EmotionsTroublesResponseSerializer
    def get(self,request,pk):
        trouble_info = EmotionsTroubles.objects.filter(id=pk).first()
        serializer1 = EmotionsTroublesSerializer(trouble_info)

        trouble_response_info = EmotionsTroublesResponse.objects.filter(emotion_trouble_id=pk)
        serializer2 = self.get_serializer(trouble_response_info, many=True)

        return Response({
            'code': 200,
            'msg':'ok',
            'data':{
                'info':serializer1.data,
                'response_info':serializer2.data
            }
        })
class Web1PushEmotionTroublesResponseView(GenericAPIView):
    queryset = EmotionsTroublesResponse.objects.all()
    serializer_class = EmotionsTroublesResponseSerializer
    def post(self,request):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            # print(data)
            data2 = EmotionsTroublesResponse(id=None, uid=data['uid'], emotion_trouble_id=data['emotion_trouble_id'],
                                           content=data['content'], up_count=data['up_count'],
                                           publish_date=data['publish_date'])
            # print(data2)
            data2.save()
            return Response({
                'code':200,
                'msg':'ok'
            })
        else:
            return Response({
                'code':404,
                'msg':serializer.error
            })
class Web1GetStudyPressureView(GenericAPIView):
    queryset = StudyPressure.objects.all()
    serializer_class = StudyPressureSerializer
    def get(self,*args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset,many=True)

        return Response({
            'code': 200,
            'msg':'ok',
            'data':serializer.data
        })
class Web1PushStudyPressureView(GenericAPIView):
    queryset = StudyPressure.objects.all()
    serializer_class = StudyPressureSerializer
    def post(self,request):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            data2 = StudyPressure(id=None, uid=data['uid'], content=data['content'],
                                     publish_date=data['publish_date'])
            data2.save()
            print(data)

            return Response({
                'code':200,
                'msg':'ok'
            })
        else:
            return Response({
                'code':404,
                'msg':serializer.error
            })
class Web1GetStudyPressureResponseView(GenericAPIView):
    queryset = StudyPressureResponse.objects.all()
    serializer_class = StudyPressureResponseSerializer
    def get(self,request, pk):
        pressure_info = StudyPressure.objects.filter(id=pk).first()
        serializer1 = StudyPressureSerializer(pressure_info)

        pressure_response_info = StudyPressureResponse.objects.filter(study_pressure_id=pk)
        serializer2 = self.get_serializer(pressure_response_info, many=True)

        return Response({
            'code': 200,
            'msg':'ok',
            'data':{
                'info': serializer1.data,
                'response_info': serializer2.data
            }
        })
class Web1PushStudyPressureResponseView(GenericAPIView):
    queryset = StudyPressureResponse.objects.all()
    serializer_class = StudyPressureResponseSerializer
    def post(self,request):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            # print(data)
            data2 = StudyPressureResponse(id=None, uid=data['uid'], study_pressure_id=data['study_pressure_id'],
                                             content=data['content'], up_count=data['up_count'],
                                             publish_date=data['publish_date'])
            # print(data2)
            data2.save()
            return Response({
                'code':200,
                'msg':'ok'
            })
        else:
            return Response({
                'code':404,
                'msg':serializer.error
            })
class Web1GetWorkPressureView(GenericAPIView):
    queryset = WorkPressure.objects.all()
    serializer_class = WorkPressureSerializer
    def get(self,*args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset,many=True)

        return Response({
            'code': 200,
            'msg':'ok',
            'data':serializer.data
        })
class Web1PushWorkPressureView(GenericAPIView):
    queryset = WorkPressure.objects.all()
    serializer_class = WorkPressureSerializer
    def post(self,request):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            data2 = WorkPressure(id=None, uid=data['uid'], content=data['content'],
                                  publish_date=data['publish_date'])
            data2.save()
            print(data)
            return Response({
                'code':200,
                'msg':'ok'
            })
        else:
            return Response({
                'code':404,
                'msg':serializer.error
            })
class Web1GetWorkPressureResponseView(GenericAPIView):
    queryset = WorkPressureResponse.objects.all()
    serializer_class = WorkPressureResponseSerializer
    def get(self,request, pk):
        pressure_info = WorkPressure.objects.filter(id=pk).first()
        serializer1 = WorkPressureSerializer(pressure_info)

        pressure_response_info = WorkPressureResponse.objects.filter(work_pressure_id=pk)
        serializer2 = self.get_serializer(pressure_response_info, many=True)

        return Response({
            'code': 200,
            'msg':'ok',
            'data':{
                'info': serializer1.data,
                'response_info': serializer2.data
            }
        })
class Web1PushWorkPressureResponseView(GenericAPIView):
    queryset = WorkPressureResponse.objects.all()
    serializer_class = WorkPressureResponseSerializer
    def post(self,request):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            # print(data)
            data2 = WorkPressureResponse(id=None, uid=data['uid'], work_pressure_id=data['work_pressure_id'],
                                          content=data['content'], up_count=data['up_count'],
                                          publish_date=data['publish_date'])
            # print(data2)
            data2.save()
            return Response({
                'code':200,
                'msg':'ok'
            })
        else:
            return Response({
                'code':404,
                'msg':serializer.error
            })

class Web1GetPsychologicalConsultantView(GenericAPIView):
    queryset = PsychologicalConsultant.objects.all()
    serializer_class = PsychologicalConsultantSerializer

    def get(self, request, cid):
        queryset = PsychologicalConsultant.objects.filter(category_id=cid)
        serializer = self.get_serializer(queryset, many=True)

        return Response(
            {
                'code':200,
                'msg': 'ok',
                'data': serializer.data
            }
        )